import os
import json

class ImageProperties:
    """获取镜像属性"""

    image_id = None
    image_name = None
    image_tag = None
    imaeg_size = None

    def __init__(self, image_id=str):
        self.get_properties(image_id)

    def get_properties(self, image_id=str):
        """根据 image_id 获取镜像内容"""

        result = os.popen("docker inspect {} 2> /dev/null".format(image_id))
        image_info_list = result.read().strip()
        if image_info_list == '[]':
            return None
        image_info = json.loads(image_info_list)[0]

        self.image_id = image_id
        self.image_name = str(image_info['RepoTags'][0]).split(":")[0]
        self.image_tag = str(image_info['RepoTags'][0]).split(":")[1]
        self.image_size = image_info['Size']
